﻿using Assets.Script;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Assets
{
    public class AdditionSprite : MonoBehaviour, IPointerClickHandler
    {
        public Text showReleverGoldText,nameText,nowStateText;
        public Image bgIcon;
        public GameObject isMounthDown;
        AdditionData m_data;

        public int nowReleverGold, nowState;
        bool isAdd = true;
        PropType type;
        internal void RefureData(AdditionData additionData)
        {
            m_data = additionData;
            bgIcon.sprite = Resources.Load<Sprite>(m_data.bgIcon);
            nowReleverGold = m_data.releveNum;
            nowState = m_data.nowLevel;
            isAdd = m_data.isAdd;
            showReleverGoldText.text = nowReleverGold.ToString();
            nameText.text = m_data.name;
            nowStateText.text = "Lv+" + nowState;
            isMounthDown.SetActive(!isAdd);
            type = (PropType)m_data.type;
        }

        internal void RefureIsBuy(bool v)
        {
            isAdd = v;
            isMounthDown.SetActive(!isAdd);
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (!isMounthDown.activeSelf)
            {
                MessageCenter.Ins.BuredMess(MessId.RefureCurrencyData, new CurrencyData
                {
                    zuanshi = 0,
                    gold = -nowReleverGold
                });
                nowState++;
                nowReleverGold += (nowState * 50);
                RefureGoldShow();
                if (type == PropType.level)
                {
                    MessageCenter.Ins.BuredMess(MessId.RefureNowPlayerLevelData,1);
                }
            }
            else
            {
                StartCoroutine(PlayeVideo());
            }
        }
        IEnumerator PlayeVideo()
        {
            UIManager.Ins.OpenUI(UIName.AdvertisingPanel);
            yield return new WaitForSeconds(5.5f);
            UIManager.Ins.CloseUI(UIName.AdvertisingPanel);
            nowState++;
            RefureGoldShow();
            if (type == PropType.level)
            {
                MessageCenter.Ins.BuredMess(MessId.RefureNowPlayerLevelData, 1);
            }
        }

        private void RefureGoldShow()
        {
            showReleverGoldText.text = nowReleverGold.ToString();
            nowStateText.text = "Lv+" + nowState;
        }
    }
}
